package com.example.myapplication.database;

import android.util.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtil {
    private static String diver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://10.1.62.214:3306/my_test?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC";
    private static String user = "root";//用户名
    private static String password = "123456";//密码


    /*
     * 连接数据库
     * */
    public Connection getConn() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection(url, user, password);//获取连接
            Log.e("getConn", "连接成功");
        } catch (ClassNotFoundException e) {
            Log.e("getConn", e.getMessage(), e);
            e.printStackTrace();
        } catch (SQLException e) {
            Log.e("getConn", e.getMessage(), e);
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
